package com.tydh.webservice.controller;


import com.alibaba.csb.sdk.HttpCaller;
import com.google.gson.Gson;
import com.tydh.webservice.bean.FacilityNewScanRecUnloadByCsbBean;
import com.tydh.webservice.bean.ParamSettingBean;
import com.tydh.webservice.bean.SortScanInfoCheckByBillByCsbBean;
import com.tydh.webservice.common.Assert;
import com.tydh.webservice.common.utils.AllSetParam;
import com.tydh.webservice.common.utils.Interfacetype;
import com.tydh.webservice.result.R;
import com.tydh.webservice.result.ResultEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 揽投部站勾核接口
 */

@SuppressWarnings("all")
@RestController
@CrossOrigin
@RequestMapping("/all/api")
@Slf4j
public class ServiceAllInterface {

    @Autowired
    private AllSetParam allSetParam;

    @Autowired
    private ParamSettingBean paramSettingBean;

    /**
     * 3.5.3	【处理中心模式扫描勾核】接口(新)
     * @param SortScanInfoCheckByBillByCsbBean
     * @return
     */
    @RequestMapping("/sortScanInfoCheckByBillByCsb")
    public R getSortScanInfoCheckByBillByCsb(@RequestBody SortScanInfoCheckByBillByCsbBean sortScanInfoCheckByBillByCsbBean)
    {
        Gson gson = new Gson();
        Map<String,String> params = new HashMap<String,String>();


        Map messageHeader = allSetParam.setMessageHeader(gson.toJson(sortScanInfoCheckByBillByCsbBean));

        String jsonMessageHeader = gson.toJson(messageHeader);
        params.put("messageHeader", jsonMessageHeader);

        String singleReceivingCheck = gson.toJson(sortScanInfoCheckByBillByCsbBean);
        params.put("singleReceivingCheck", singleReceivingCheck);

        String result;
        try {
            result = HttpCaller.doPost(Interfacetype.URL, Interfacetype.SORTSCANINFO_NAME, paramSettingBean.getVersion(), params, paramSettingBean.getAk(), paramSettingBean.getSk());
            if (result != null) {
                result = result.replaceAll("\\\\","");
            }
            Assert.notNull(result, ResultEnum.ALIYUN_RESPONSE_FAIL);
            log.info("新一代的数据{}", result);
            return  R.ok().data("data", result);
        } catch (Exception ie) {
            log.info(ie.getMessage());
            return R.error().data("error", ie.getMessage());
        }
    }


    /**
     * CSB-中转-揽投部站外接设备扫描接口
     * @param facilityNewScanRecUnloadByCsbBean
     * @return
     */
    @RequestMapping("/dlvUnseaRecieveService")
    public R getSolicitInvestment(@RequestBody FacilityNewScanRecUnloadByCsbBean facilityNewScanRecUnloadByCsbBean)
    {
        Gson gson = new Gson();
        Map<String,String> params = new HashMap<String,String>();

        String baseParams = allSetParam.getBaseParamsByBean(facilityNewScanRecUnloadByCsbBean);
        Map messageHeader = allSetParam.setMessageHeader(baseParams);

        //messageHeader转为json
        String jsonMessageHeader = gson.toJson(messageHeader);
        params.put("messageHeader", jsonMessageHeader);
        params.put("opOrgCode", facilityNewScanRecUnloadByCsbBean.getOpOrgCode());
        params.put("waybillNo", facilityNewScanRecUnloadByCsbBean.getWaybillNo());
        params.put("handoverObjectNo", facilityNewScanRecUnloadByCsbBean.getHandoverObjectNo());
        params.put("truckingNo", facilityNewScanRecUnloadByCsbBean.getTruckingNo());
        params.put("billNo", facilityNewScanRecUnloadByCsbBean.getBillNo());

        String result;
        try {
            result = HttpCaller.doPost(Interfacetype.URL, Interfacetype.LANTOU_API_NAME, paramSettingBean.getVersion(), params, paramSettingBean.getAk(), paramSettingBean.getSk());
            if (result != null) {
                result = result.replaceAll("\\\\","");
            }
            log.info("新一代的数据{}", result);
            Assert.notNull(result, ResultEnum.ALIYUN_RESPONSE_FAIL);
            return  R.ok().data("data", result);
        } catch (Exception ie) {
            log.info(ie.getMessage());
            return R.error().data("error", ie.getMessage());
        }
    }
}
